package com.togo.algorithm.easy.string;

/**
 * @Author taiyn
 * @Description TODO
 * @Date 11:17 AM 2023/5/24
 **/
public class Offer58 {

    public String reverseWords(String s) {

        if(s == null || s.isEmpty()) return s;

        String ss = s.trim();
        String[] arr = ss.split(" ");

        int i = arr.length - 1;
        StringBuilder sb = new StringBuilder();
        while(i > 0) {
            if(!arr[i].isEmpty()) {
                sb.append(arr[i]);
                sb.append(" ");
            }

            i--;
        }

        sb.append(arr[0]);

        return sb.toString();
    }

    public static void main(String[] args) {
        Offer58 offer58 = new Offer58();
        String s = "a good   example";
        System.out.println(offer58.reverseWords(s));;
    }
}
